//
//  ESFacialPanelController.m
//  SportsGather
//
//  Created by fuyoufang on 15/10/17.
//  Copyright (c) 2015年 Egymcom. All rights reserved.
//

#import "ESFacialPanelController.h"
#import "NSTextAttachment+Util.h"
#import <objc/runtime.h>
#import "ESFacialCollectionViewCell.h"

@interface ESFacialPanelController ()<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *emojiCollectionView;

@end

@implementation ESFacialPanelController

#pragma mark - 懒加载

- (UICollectionView *)emojiCollectionView{
    if (_emojiCollectionView == nil) {
        UICollectionViewFlowLayout *flowLayout = [UICollectionViewFlowLayout new];
        CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
        flowLayout.minimumInteritemSpacing = (screenWidth - 40 - 30 * 7) / 7;
        flowLayout.minimumLineSpacing = 25;
        flowLayout.sectionInset = UIEdgeInsetsMake(4, 0, 2, 0);
        
        _emojiCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        [_emojiCollectionView registerClass:[ESFacialCollectionViewCell class] forCellWithReuseIdentifier:FacialCellReuseIdentifier];
        _emojiCollectionView.backgroundColor = [UIColor clearColor];
        _emojiCollectionView.scrollEnabled = NO;
        _emojiCollectionView.dataSource = self;
        _emojiCollectionView.delegate = self;
        _emojiCollectionView.bounces = NO;
    }
    return _emojiCollectionView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor lightGrayColor];
    [self setupEmojiCollectionView];
}

- (void)setupEmojiCollectionView {
    [self.view addSubview:self.emojiCollectionView];
    self.emojiCollectionView.translatesAutoresizingMaskIntoConstraints = NO;
    NSDictionary *views = NSDictionaryOfVariableBindings(_emojiCollectionView);
    
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-8-[_emojiCollectionView]|" options:0 metrics:nil views:views]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-20-[_emojiCollectionView]-20-|" options:0 metrics:nil views:views]];
}

- (void)setFacials:(NSArray *)facials {
    _facials = facials;
    [self.emojiCollectionView reloadData];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return (self.facials.count + 7) / 7;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return (self.facials.count - 7 * section) > 7 ? 7 : (self.facials.count - 7 * section);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(30, 30);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ESFacialCollectionViewCell *cell = [ESFacialCollectionViewCell cellWith:collectionView forIndexPath:indexPath];
    NSInteger section = indexPath.section;
    NSInteger row     = indexPath.row;
    cell.facial = [self.facials objectAtIndex:(section * 7 + row)];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger section = indexPath.section;
    NSInteger row     = indexPath.row;
    
//   
//        NSInteger emojiNum = _pageIndex * 20 + section * 7 + row + 1;
//        NSString *emojiImageName, *emojiStr;
//        if (emojiNum >= 106) {
//            emojiStr = @"tt";
//            //emojiStr = Utils.emojiDict[@(emojiNum).stringValue];
//            emojiImageName = [emojiStr stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@":"]];
//        } else {
//            emojiStr = [NSString stringWithFormat:@"[%ld]", emojiNum - 1];
//            emojiImageName = [NSString stringWithFormat:@"%03ld", emojiNum];
//        }
//        
//        NSTextAttachment *textAttachment = [NSTextAttachment new];
//        textAttachment.image = [UIImage imageNamed:emojiImageName];
//        [textAttachment adjustY:-3];
//#warning TODO 这一句是什么意思,需要包含 #import <objc/runtime.h>
//        objc_setAssociatedObject(textAttachment, @"emoji", emojiStr, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
//        
//        _didSelectEmoji(textAttachment);
    
}


@end
